<?php
/**
 * User: bim bip
 * Date: 7/29/11
 * Time: 1:22 PM
 */
class Ad_Movie extends CI_Controller{
    var $_imageDir = 'uploads/images/';
    var $_tempDir = 'uploads/temp/';
    var $_imageDefault = 'uploads/images/default.jpg';
    
    function __construct(){
        parent::__construct();
        if(!$this->session->userdata('logged_in')){
            echo "You must refresh your browser to be continute!!!";
            die;
        }
        //create images dir
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $today = date('dmY');
        if(!is_dir("uploads/images/$today/")){
            //create new dir
            mkdir("uploads/images/$today/");
            $this->_imageDir = "uploads/images/$today/";
        }else
            $this->_imageDir = "uploads/images/$today/";
            
    }

    function search(){
        if($this->input->post('search')){
            $name = $this->input->post('keywords');
            //load movie
            $this->load->model('movie/m_movie');
            $data['movieList'] = $this->m_movie->search($name);
            $this->load->view('manage/movie/search', $data);
        }else{
            $this->load->view('manage/movie/search');
        }

    }

    //new video
    function addNew(){
        //check do add new
        if($this->input->post('addnew-submit')){
            $movieID = $this->_doAddNew();
            redirect('admincp/movie/update/'.$movieID,'location',302);
        }
        $this->load->helper('ckeditor');
        $this->load->model('movie/m_category');
        $data = array();
        $data['categoryList'] = $this->m_category->getCategoryList();
        $data['editor'] = displayFullEditor('introduction', 800, 300);
        
        $this->load->view('manage/movie/new_movie', $data);
    }

    /**
     * @return int
     */
    function _doAddNew(){
        if(!$this->input->post('addnew-submit')
           || !$this->input->post('txt_name'))
            return;
        //lay post
        $name = $this->input->post('txt_name');
        $categoryID = $this->input->post('cbx_category');
        $main_content = $this->input->post('main_content');
        $introduction = $this->input->post('introduction');
        $actors = $this->input->post('txt_actors');
        $year = $this->input->post('txt_year');

        //config image
        $image = $this->_imageDefault; //image default
    //load thu vien de kiem tra image
        $config = array();
        $config['upload_path'] = $this->_tempDir;
        $config['allowed_types'] = 'gif|jpg|jpeg|bmp|png'; //file type
        $config['max_size'] = '2048'; //max file size
        $config['encrypt_name'] = 'TRUE'; //encrypt file

        $this->load->library('upload', $config);
        if($this->upload->do_upload('image_file') == FALSE){ //co loi trong qua trinh upload hoac ko co file
        }else{
            $img = $this->upload->data();
            //create thumb
            if($this->_createThumb($img['full_path']))
                $image = $this->_imageDir . $img['raw_name'] . '_thumb' . $img['file_ext'];
        }


        $this->load->model('movie/m_movie');
        $movie = new M_Movie();
        $movie->_name = $name;
        $movie->_actors = $actors;
        $movie->_main_content = $main_content;
        $movie->_introduction = $introduction;
        $movie->_movie_category_id = $categoryID;
        $movie->_year = $year;
        $movie->_image = $image;
        $movie->_date = date('Y-m-d');
        //add movie
        $movieID = $movie->addMovie();

        //add default episode
        $this->load->model('movie/m_episode');
        $episode = new M_Episode();
        $episode->_movie_id = $movieID;
        $episode->_path = 'u add link here';
        $episode->_name = 'update lai tap film nay';
        $episode->addNew();

        //delete cache
        $this->db->cache_delete('movie', 'search');
        $this->db->cache_delete('movie', 'searchcategory');
        $this->db->cache_delete('movie', 'index');

        return $movieID;
    }
    //update video

    function update($movieID){
        //check do update
        if($this->input->post('update-submit')){
            $this->_doUpdate();
            redirect('admincp/movie/update/' . $movieID, 'location', '301');
        }
        //kiem tra ko ton tai ID thi bao loi

        $data = array();
        //load heper and model
        $this->load->helper('ckeditor');
        $this->load->model(array(
                               'movie/m_movie',
                               'movie/m_episode',
                               'movie/m_category'
                           ));

        //load movie info
        $movieInfo = $this->m_movie->getMovieInfo($movieID);
        /**
         * kiem tra neu ko ton tai movie nay trong csdl thi redirect den trang bao loi
         */
        if(count($movieInfo) == 0)
            redirect('', 'location', 302);
        else
            $data['movieInfo'] = $movieInfo;

       //load movie category
        $data['categoryList'] = $this->m_category->getCategoryList();
        $data['movieID'] = $movieID;
        //load editor
        $data['editor'] = displayFullEditor('introduction', 800, 300);

        $this->load->view('manage/movie/update_movie', $data);
    }

    /**
     * update movie nay
     * @return void
     */
    function _doUpdate(){
        $this->load->model('movie/m_movie');
        if(!$this->input->post('update-submit'))
            return;
        //lay post
        $movieID = $this->input->post('id');
        $name = $this->input->post('txt_name');
        $categoryID = $this->input->post('cbx_category');
        $main_content = $this->input->post('main_content');
        $introduction = $this->input->post('introduction');
        $actors = $this->input->post('txt_actors');
        $year = $this->input->post('txt_year');
        //load thu vien de kiem tra image
        $config = array();
        $config['upload_path'] = $this->_tempDir;
        $config['allowed_types'] = 'gif|jpg|jpeg|bmp|png'; //file type
        $config['max_size'] = '2048'; //max file size
        $config['encrypt_name'] = 'TRUE'; //encrypt file

        $this->load->library('upload', $config);
        if($this->upload->do_upload('imagefile') == FALSE){ //co loi trong qua trinh upload hoac ko co file
            $image = '';
            //echo $this->upload->display_errors();
        }else{
            $img = $this->upload->data();
            //create thumb
            if($this->_createThumb($img['full_path'])){ //neu tao thumb thanh cong
                $image = $this->_imageDir . $img['raw_name'] . '_thumb' . $img['file_ext'];
                echo $this->_watermarkImg($image);
                //delete old image
                $old = $this->m_movie->getMovieImage($movieID);
                if($old != $this->_imageDefault)
                    unlink($old);
            }else
                $image = '';

        }
        
        //update movie info
        $this->load->model('movie/m_movie');
        $movieInfo = array(
            'name' => $name,
            'movie_category_id' => $categoryID,
            'main_content' => htmlspecialchars($main_content),
            'introduction' => $introduction,
            'image' => $image,
            'actors' => $actors,
            'movie_id' => $movieID,
            'year' => $year
        );
        $this->m_movie->updateMovie($movieInfo);

        //delete cache
        $this->db->cache_delete('video', 'search');
        $this->db->cache_delete('video', 'searchcategory');
        $this->db->cache_delete('video', 'searchactor');
        $this->db->cache_delete('video', 'index');
    }

    /**
     * create img thumb
     * @param  $img
     * @return bool
     */
    function _createThumb($img){
        $config['image_library'] = 'gd';
        $config['source_image'] = $img;
        $config['new_image'] = $this->_imageDir;
        $config['create_thumb'] = TRUE;
        //$config['maintain_ratio'] = TRUE;
        //$config['width'] = 200;
        //$config['height'] = 300;
        //$config['quality'] = 100;
        $this->load->library('image_lib', $config);
        //resize image
        if(! $this->image_lib->resize())
            return false;
        else{
            //delete image temp
            unlink($img);
            return true;
        }
    }

    function _watermarkImg($img){
        //load image_moo
        $this->load->library('image_moo');
        $this->image_moo
                        ->load($img)
                        ->make_watermark_text("MatMoo.com", "fonts/texb", 25, "#000")
                        ->resize(400,400)
                        ->watermark(2)
                        ->save_dynamic();
        if($this->image_moo->errors)
            return 'tot';
        else{
            return $this->image_moo->display_errors();

        }
    }
    /**
     * choice list - slide show
     *
     */
    function choiceList(){
        $this->load->model('movie/m_choice');
        $data['list'] = $this->m_choice->getChoiceMoviesList();
        $this->load->view('manage/movie/choice_list', $data);
    }
        
    function addNewChoice(){
        $movieID = $this->input->get_post('id');
        if(!$movieID){
            echo 'you kid me??';
            return;
        }
        $this->load->model('movie/m_choice');
        $choice = new M_Choice();
        $choice->movie_id = $movieID;
        if($choice->isExist()){
            echo 'the movie is exist';
            return false;
        } //if movie  exist
        else{
            $choice->addNew();
            echo 'add successful';
            return;
        }

        //delete cache
        $this->db->cache_delete('video', 'index');
    }

    function deleteChoice(){
        $movieID = $this->input->get_post('id');
        if(!$movieID) {
            echo 'you kid me?';
            return;
        }
        $this->load->model('movie/m_choice');
        $this->m_choice->doDelete($movieID);
        echo 'success';

        //delete cache
        $this->db->cache_delete('video', 'index');
    }

}
 
